package com.xiaocg.base.thread.pool;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xiaocg
 */
public class ThreadPoolDemo {
    public static void main(String[] args)throws Exception {

        Executors.newFixedThreadPool(5);
        Executors.newSingleThreadExecutor();
        Executors.newCachedThreadPool();

        //阻塞请求队列长度为 为Integer.MAX_VALUE 造成资源不可控

        ExecutorService executorService = new ThreadPoolExecutor(5, 10, 0,
                TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new ThreadFactory() {
            private AtomicInteger atomicInteger = new AtomicInteger(0);
            @Override
            public Thread newThread(Runnable r) {
                int andIncrement = atomicInteger.incrementAndGet();
                return new WorkThread(r,atomicInteger);
            }
        });

        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());
        executorService.execute(new Task());

        executorService.shutdown();


    }
}
